package com.syzg.base.operation.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.syzg.common.config.SysProperties;
import com.syzg.common.controller.BaseController;
import com.syzg.common.model.WebInfo;
import com.syzg.common.util.SearchMap;
import com.syzg.common.util.MethodUtil;
import com.syzg.common.model.WebObj;
import com.syzg.common.util.PKCreator;
import com.syzg.common.exception.BaseException;
import com.syzg.core.page.PageResult;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.syzg.base.merge.model.Module;
import com.syzg.base.merge.model.Operation;
import com.syzg.base.operation.service.OperationService;

@Controller
@RequestMapping("/path/operation")
public class OperationController extends BaseController{
	
	static Logger log = LoggerFactory.getLogger(OperationController.class);
	
	@Resource
	private OperationService operationService;
	
	@RequestMapping("/listOperation")
	public ModelAndView listOperation(WebObj webObj)throws Exception{
		ModelAndView mv = new ModelAndView("/base/jsp/operation/listOperation.jsp");
		SearchMap searchMap = SearchMap.forClass(Operation.class);
		PageResult pageResult = operationService.findObjectsForPage(webObj.getPageNo(), SysProperties.PAGESIZE, searchMap);
        webObj.setPageResult(pageResult);
        mv.addObject("webObj",webObj);
        return mv;
	}
	
	@RequestMapping("/initOperation")
	public ModelAndView initOperation() throws Exception {
		ModelAndView mv = new ModelAndView("/base/jsp/operation/addOperation.jsp");
		try {
			mv.addObject("operation", new Operation());
			/*LoginObject loginObj = SystemUtil.getLoginObject();
			this.operation.setCreatePersonId(new String(loginObj.getPersonId()));
			this.operation.setCreator(loginObj.getPersonName());
			this.operation.setCreateTime(new Date());*/
			SearchMap searchMap = SearchMap.forClass(Module.class);
			searchMap.notEq("a.href", "#");
	    	searchMap.notEq("a.id", "0");
	    	searchMap.addOrder("a.parentModule.id", SearchMap.ORDER_ASC);
	    	searchMap.addOrder("a.orderNo", SearchMap.ORDER_ASC);
	    	mv.addObject("moduleList", operationService.findObjects(searchMap));
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/addOperation")
	public @ResponseBody String addOperation(Operation operation) throws Exception {
		WebObj webObj = new WebObj();
		try {
			/*LoginObject loginObj = SystemUtil.getLoginObject();
			this.operation.setCreatePersonId(new String(loginObj.getPersonId()));
			this.operation.setCreator(loginObj.getPersonName());
			this.operation.setCreateTime(new Date());*/
			operation.setId(PKCreator.getPk());
			operationService.create(operation);
			setControllerStatus(webObj,WebInfo.STATUS_SUCCESS);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
			setControllerStatus(webObj,WebInfo.STATUS_FAILURE);
			webObj.getWebInfo().getErrorList().add("操作失败");
		}
		return new JSONObject(webObj).toString();
	}
	
	@RequestMapping("/viewOperation")
	public ModelAndView viewOperation(Operation operation,ModelAndView mv) throws Exception {
		mv.setViewName("/base/jsp/operation/viewOperation.jsp");
		try {
			String id = operation.getId();
			Operation operationVO = operationService.findObjectById(id);
			mv.addObject("operation",operationVO);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/editOperation")
	public ModelAndView editOperation(String id) throws Exception {
		ModelAndView mv = new ModelAndView("/base/jsp/operation/editOperation.jsp");
		try {
			Operation operation = operationService.findObjectById(id);
			mv.addObject("operation", operation);
			SearchMap searchMap = SearchMap.forClass(Module.class);
			searchMap.notEq("a.href", "#");
	    	searchMap.notEq("a.id", "0");
	    	searchMap.addOrder("a.parentModule.id", SearchMap.ORDER_ASC);
	    	searchMap.addOrder("a.orderNo", SearchMap.ORDER_ASC);
	    	mv.addObject("moduleList", operationService.findObjects(searchMap));
			WebObj webObj = new WebObj();
			mv.addObject("webObj",webObj);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/modOperation")
	public @ResponseBody String modOperation(String id,Operation operationVO) throws Exception {
		WebObj webObj = new WebObj();
		try {
			Operation operation = operationService.findObjectById(id);
			operation = (Operation) MethodUtil.copyProperties(operation, operationVO);
			operationService.update(operation);
			setControllerStatus(webObj,WebInfo.STATUS_SUCCESS);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
			setControllerStatus(webObj,WebInfo.STATUS_FAILURE);
			webObj.getWebInfo().getErrorList().add("操作失败");
		}
		return new JSONObject(webObj).toString();
	}
	
	@RequestMapping("/delOperation")
	public ModelAndView delOperation(String id,WebObj webObj) throws Exception {
		String[] ids = webObj.getSelectedRow();
		try {
			if(ids == null) { //删除单个
				if(id == null) {
					ids = new String[0];
				} else {
					ids = new String[]{String.valueOf(id)};
				}
			}
			String[] idsStr = new String[ids.length];
			int i = 0;
			for(String idVO: ids) {
				idsStr[i++] = new String(idVO);
			}
			operationService.delete(idsStr);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return listOperation(webObj);
	}
}
